clear all
macro drop _all
set more off

*ssc install excelcol

********************************************************************************
*				WORKING DIRECTORIES AND SETTINGS
********************************************************************************

if ("`c(os)'"=="Windows") global ROOT "C:/Users/`c(username)'/Dropbox/PhD Maria Ulugbek Mapping indiv poverty" //Windows
if ("`c(os)'"=="MacOSX") global ROOT "/Users/`c(username)'/Dropbox/PhD Maria Ulugbek Mapping indiv poverty" //Mac

global origdata 	"$ROOT/2-data/1-raw"
global cultdata		"$ROOT/2-data/0-cultural data/Data"

global readydata  	"$ROOT/3-prog/CULTURE/replication/1_data"
global prog		 	"$ROOT/3-prog/CULTURE/replication/2_dofiles"
global shares		"$ROOT/3-prog/CULTURE/replication/3_shares"
global tables		"$ROOT/3-prog/CULTURE/replication/4_tables"
global figures		"$ROOT/3-prog/CULTURE/replication/5_figures"
		
* Graph settings
set scheme s2color 
graph set window fontface "Helvetica"
graph set eps fontface Helvetica

grstyle init
grstyle color background white
grstyle anglestyle vertical_tick horizontal

** Colors: grayscale
global warm "gs5"
global cold "gs10"

** Size of labels
global labsize "*.8"

global GHAlab "Ghana"
global MWIlab "Malawi"
global GHAlabp "(A) Ghana"
global MWIlabp "(B) Malawi"


********************************************************************************
* 			OPTIONS
********************************************************************************

* Output: 
scalar output = 1
* 1 = Table 1: Matching results
* 2 = Table 2: Baseline results - resource shares and marginal effects (model=1)
* 3 = Figure 1: Distribution of women's resource shares (model=1)
* 4 = Figure 2: Women's resource share by age (model=3)
* 5 = Figure 3: Heterogeneity of women's control 
* 6 = Figure 4 and A1: Poverty effects of patrilocality (model=1)
* 7 = Table A1: Summary statistics by HH type 
* 8 = Table A2: Summary statistics by patrilocality
* 9 = Table A3: Robustness check (model=1, model=2, model=3)
* 10 = Table A4: Robustness check - patrilineality (model=4)
* 11 = Table A5: Poverty estimates, $1.9 (model=1)

*----------------- Table 1: Results of matching with cultural data ----------------- *
if output ==1 {
	
	putexcel set "${tables}/Tab1.xlsx", replace
	putexcel A1="Country" B1="Matching variables" C1="Patrilocal" D1="Matrilocal" E1="Neo/ambilocal"
	putexcel A2="Ghana" B2="Declared ethnicity" 
	putexcel A3="Malawi" B3="Declared language"
	putexcel A4="Ghana" B4="Sub-national region"
	putexcel A5="Malawi" B5="GPS location" 
	
	* Ghana
	use "${shares}/GHA_model1.dta", clear
	foreach x in patrilocal matrilocal neolocal patrilocal_geo matrilocal_geo neolocal_geo  {
		sum `x' if kfm==1
		local `x'_kfm = `r(mean)'
		local N_kfm = `r(N)'
		sum `x' if fm==1
		local `x'_fm = `r(mean)'
		local N_fm = `r(N)'
	}
	local patrilocal_all: disp %3.1f 100*(`patrilocal_kfm'*`N_kfm'+`patrilocal_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local matrilocal_all: disp %3.1f 100*(`matrilocal_kfm'*`N_kfm'+`matrilocal_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local neolocal_all: disp %3.1f   (100-`patrilocal_all'-`matrilocal_all')
	*local neolocal_all: disp %3.1f 100*(`neolocal_kfm'*`N_kfm'+`neolocal_fm'*`N_fm')/(`N_kfm'+`N_fm')
	*local matrilocal_all: disp %3.1f   (100-`patrilocal_all'-`neolocal_all')
	
	disp "`patrilocal_all'"
	disp "`matrilocal_all'"
	disp "`neolocal_all'"
	
	local patrilocal_geo_all: disp %3.1f 100*(`patrilocal_geo_kfm'*`N_kfm'+`patrilocal_geo_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local matrilocal_geo_all: disp %3.1f 100*(`matrilocal_geo_kfm'*`N_kfm'+`matrilocal_geo_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local neolocal_geo_all: disp %3.1f   (100-`patrilocal_geo_all'-`matrilocal_geo_all')	
	*local neolocal_geo_all: disp %3.1f 100*(`neolocal_geo_kfm'*`N_kfm'+`neolocal_geo_fm'*`N_fm')/(`N_kfm'+`N_fm')
	*local matrilocal_geo_all: disp %3.1f (100-`patrilocal_geo_all'-`neolocal_geo_all')
	
	disp "`patrilocal_geo_all'"
	disp "`matrilocal_geo_all'"
	disp "`neolocal_geo_all'"
	
	putexcel C2 = `patrilocal_all'
	putexcel D2 = `matrilocal_all'
	putexcel E2 = `neolocal_all'
	
	putexcel C4 = `patrilocal_geo_all'
	putexcel D4 = `matrilocal_geo_all'
	putexcel E4 = `neolocal_geo_all'
	
	
	* Malawi
	use "${shares}/MWI_model1.dta", clear
	foreach x in patrilocal matrilocal neolocal patrilocal_geo matrilocal_geo neolocal_geo  {
		sum `x' if kfm==1
		local `x'_kfm = `r(mean)'
		local N_kfm = `r(N)'
		sum `x' if fm==1
		local `x'_fm = `r(mean)'
		local N_fm = `r(N)'
	}
	local patrilocal_all: disp %3.1f 100*(`patrilocal_kfm'*`N_kfm'+`patrilocal_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local matrilocal_all: disp %3.1f 100*(`matrilocal_kfm'*`N_kfm'+`matrilocal_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local neolocal_all: disp %3.1f   (100-`patrilocal_all'-`matrilocal_all')
	*local neolocal_all: disp %3.1f 100*(`neolocal_kfm'*`N_kfm'+`neolocal_fm'*`N_fm')/(`N_kfm'+`N_fm')
	*local matrilocal_all: disp %3.1f   (100-`patrilocal_all'-`neolocal_all')
	
	disp "`patrilocal_all'"
	disp "`matrilocal_all'"
	disp "`neolocal_all'"
	
	local patrilocal_geo_all: disp %3.1f 100*(`patrilocal_geo_kfm'*`N_kfm'+`patrilocal_geo_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local matrilocal_geo_all: disp %3.1f 100*(`matrilocal_geo_kfm'*`N_kfm'+`matrilocal_geo_fm'*`N_fm')/(`N_kfm'+`N_fm')
	local neolocal_geo_all: disp %3.1f   (100-`patrilocal_geo_all'-`matrilocal_geo_all')	
	*local neolocal_geo_all: disp %3.1f 100*(`neolocal_geo_kfm'*`N_kfm'+`neolocal_geo_fm'*`N_fm')/(`N_kfm'+`N_fm')
	*local matrilocal_geo_all: disp %3.1f (100-`patrilocal_geo_all'-`neolocal_geo_all')
	
	disp "`patrilocal_geo_all'"
	disp "`matrilocal_geo_all'"
	disp "`neolocal_geo_all'"
	
	putexcel C3 = `patrilocal_all'
	putexcel D3 = `matrilocal_all'
	putexcel E3 = `neolocal_all'
	
	putexcel C5 = `patrilocal_geo_all'
	putexcel D5 = `matrilocal_geo_all'
	putexcel E5 = `neolocal_geo_all'
	
	putexcel close
}


*----------------- Table 2: Baseline results ----------------- *
if output ==2 {
		
	putexcel set "${tables}/Tab2.xlsx", replace
	
	putexcel B1="Ghana" 
	putexcel B1:C1, merge hcenter
	putexcel D1="Malawi" 
	putexcel D1:E1, merge hcenter
	putexcel B2 = "children, women and men"
	putexcel C2 = "women and men" 
	putexcel D2 = "children, women and men"
	putexcel E2 = "women and men" 
	
	putexcel A3 = "(a) Resource shares" 
	putexcel A4 = "Children"
	putexcel A6 = "Women"
	putexcel A8 = "Men" 
	putexcel A11 = "Per child"
	putexcel A13 = "Per woman"
	putexcel A15 = "Per man" 
	
	putexcel A18 = "(b) Marginal effects on per person resource shares"
	putexcel A19 = "Women's resource shares:"
	putexcel A20 = "Patrilocal (=1)"
	putexcel A22 = "Proportion of boys"
	putexcel A24 = "Urban (=1)" 
	putexcel A26 = "Children's resource shares:"
	putexcel A27 = "Patrilocal (=1)" 
	putexcel A29 = "Proportion of boys"
	putexcel A31 = "Urban (=1)" 
	
	putexcel A34 = "% of HHs with non-flat Engel curve"
	putexcel A35 = "% of patrilocality"
	putexcel A36 = "N"
	

	* (a) Resource shares
	***************************
	** kfm

	local GHAcol = "B"
	local MWIcol = "D"
	foreach cc in GHA MWI {
		
		use "${shares}/`cc'_model1.dta", clear
		local mrow = 4
		local sdrow = 5
		local pmrow = 11
		local psdrow = 12
		
		foreach x in k f m {
			** per dem group
			sum share_`x' if kfm==1
			local share_`x'_kfm: disp %4.3f `r(mean)'
			local share_`x'_kfm_sd: disp %4.3f `r(sd)'	
			putexcel ``cc'col'`mrow' = `share_`x'_kfm', hcenter
			putexcel ``cc'col'`sdrow' = "(`share_`x'_kfm_sd')"	, hcenter
			local mrow=`mrow'+2
			local sdrow=`sdrow'+2
			
			** per person
			sum share_p`x' if kfm==1
			local share_p`x'_kfm: disp %4.3f `r(mean)'
			local share_p`x'_kfm_sd: disp %4.3f `r(sd)'
			putexcel ``cc'col'`pmrow' = `share_p`x'_kfm', hcenter
			putexcel ``cc'col'`psdrow' = "(`share_p`x'_kfm_sd')", hcenter	
			local pmrow=`pmrow'+2
			local psdrow=`psdrow'+2
		}
	}
	
	** fm 
	local GHAcol = "C"
	local MWIcol = "E"
	foreach cc in GHA MWI {
		
		use "${shares}/`cc'_model1.dta", clear
		local mrow = 6
		local sdrow = 7
		local pmrow = 13
		local psdrow = 14
		
		foreach x in f m {
			** per dem group
			sum share_`x' if fm==1
			local share_`x'_fm: disp %4.3f `r(mean)'
			local share_`x'_fm_sd: disp %4.3f `r(sd)'	
			putexcel ``cc'col'`mrow' = `share_`x'_fm', hcenter
			putexcel ``cc'col'`sdrow' = "(`share_`x'_fm_sd')"	, hcenter
			local mrow=`mrow'+2
			local sdrow=`sdrow'+2
			
			** per person
			sum share_p`x' if fm==1
			local share_p`x'_fm: disp %4.3f `r(mean)'
			local share_p`x'_fm_sd: disp %4.3f `r(sd)'
			putexcel ``cc'col'`pmrow' = `share_p`x'_fm', hcenter
			putexcel ``cc'col'`psdrow' = "(`share_p`x'_fm_sd')"	, hcenter
			local pmrow=`pmrow'+2
			local psdrow=`psdrow'+2
		}
	}
	
	* (b) Marginal effects
	***************************
	** kfm 
	local GHAcol = "B"
	local MWIcol = "D"
	
	foreach cc in GHA MWI {
		
		use "${shares}/`cc'_model1.dta", clear
		local b1row = 27
		local se1row = 28
		local b2row = 20
		local se2row = 21
		
		foreach var in patrilocal pboy urban {
			* Women's
			sum kfm_eta2_`var'_m
			local eta2_`var': disp %4.3f `r(mean)'
			sum kfm_eta2_`var'_mse
			local eta2_`var'_se: disp %4.3f `r(mean)'
			local eta2_`var'_st ""
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.64 & abs(`eta2_`var''/`eta2_`var'_se')<1.96) local eta2_`var'_st = "*" //10%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.96 & abs(`eta2_`var''/`eta2_`var'_se')<2.58) local eta2_`var'_st = "**" //5%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 2.58 ) local eta2_`var'_st = "***" //1%
			putexcel ``cc'col'`b2row' = "`eta2_`var''`eta2_`var'_st'", hcenter
			putexcel ``cc'col'`se2row' = "(`eta2_`var'_se')"	, hcenter
			local b2row=`b2row'+2
			local se2row=`se2row'+2	
			
			
			* Children's 
			sum kfm_eta1_`var'_m
			local eta1_`var': disp %4.3f `r(mean)'
			sum kfm_eta1_`var'_mse
			local eta1_`var'_se: disp %4.3f `r(mean)'
			local eta1_`var'_st ""
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 1.64 & abs(`eta1_`var''/`eta1_`var'_se')<1.96) local eta1_`var'_st = "*" //10%
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 1.96 & abs(`eta1_`var''/`eta1_`var'_se')<2.58) local eta1_`var'_st = "**" //5%
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 2.58 ) local eta1_`var'_st = "***" //1%
			putexcel ``cc'col'`b1row' = "`eta1_`var''`eta1_`var'_st'", hcenter
			putexcel ``cc'col'`se1row' = "(`eta1_`var'_se')"	, hcenter
			local b1row=`b1row'+2
			local se1row=`se1row'+2	
			
		}
		
	}
	
	
	** fm 
	local GHAcol = "C"
	local MWIcol = "E"
	
	foreach cc in GHA MWI {
		
		use "${shares}/`cc'_model1.dta", clear
		local b1row = 27
		local se1row = 28
		local b2row = 20
		local se2row = 21
		
		foreach var in patrilocal urban {
			* Women's
			sum fm_eta2_`var'_m
			local eta2_`var': disp %4.3f `r(mean)'
			sum fm_eta2_`var'_mse
			local eta2_`var'_se: disp %4.3f `r(mean)'
			local eta2_`var'_st ""
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.64 & abs(`eta2_`var''/`eta2_`var'_se')<1.96) local eta2_`var'_st = "*" //10%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.96 & abs(`eta2_`var''/`eta2_`var'_se')<2.58) local eta2_`var'_st = "**" //5%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 2.58 ) local eta2_`var'_st = "***" //1%
			putexcel ``cc'col'`b2row' = "`eta2_`var''`eta2_`var'_st'", hcenter
			putexcel ``cc'col'`se2row' = "(`eta2_`var'_se')"	, hcenter
			local b2row=`b2row'+4
			local se2row=`se2row'+4	
			
		}
		
	}
	
	
		
	* Other statistics
	***************************
	local GHAcol_kfm = "B"
	local MWIcol_kfm = "D"
	local GHAcol_fm = "C"
	local MWIcol_fm = "E"	
	foreach cc in GHA MWI {
		use "${shares}/`cc'_model1.dta", clear
		foreach t in kfm fm {
			local row=34
			foreach var in beta_pass patrilocal {
				sum `var' if `t'==1
				local `var'_mean: disp %4.3f `r(mean)'
				putexcel ``cc'col_`t''`row' = ``var'_mean', hcenter
				local row=`row'+1
			}
			
			count if `t'==1
			putexcel ``cc'col_`t''`row' = `r(N)', hcenter			
			
		}
	}
	
	putexcel close
	

}


*----------------- Figure 1: Distribution of women's resource shares ----------------- *
if output ==3 {

	foreach cc in GHA MWI {
		use "${shares}/`cc'_model1.dta", clear

		sum share_pf if kfm==1 & patrilocal==1, d
		local meanshare1: disp %4.3f r(mean)
		sum share_pf if kfm==1 & patrilocal==0, d
		local meanshare0: disp %4.3f r(mean)

		if (`meanshare1'>=`meanshare0') local pos1 "3"
		if (`meanshare1'>=`meanshare0') local pos0 "9"
		if (`meanshare1'<`meanshare0') local pos1 "9"
		if (`meanshare1'<`meanshare0') local pos0 "3"							

		twoway  (kdensity share_pf if patrilocal==1 & kfm==1, lcolor("$warm") lwidth(*1.4) lpattern(dash) bwidth(0.028)  ) ///
				(kdensity share_pf if patrilocal==0 & kfm==1, lcolor("$cold") lwidth(*1.4) lpattern(solid) bwidth(0.028) ) ///
				(scatteri 11 `meanshare1' (`pos1') "`meanshare1'", mlabcolor("$warm%60") ms(none)) ///
				(scatteri 11 `meanshare0' (`pos0') "`meanshare0'", mlabcolor("$cold%60") ms(none)) ///
				(scatteri 11 `meanshare1' , mcolor("$warm*.60") ms(round) msize(*.7)) ///
				(scatteri 11 `meanshare0' , mcolor("$cold*.60") ms(round) msize(*.7)) ///									
				, legend(order(1 "Patrilocal" 2 "Matrilocal") size(${labsize})  symxsize(*.7)) ytitle("Kernel Density", size(${labsize})) xtitle("Per woman resource share", size(${labsize}) margin(t+2 b+1)) ///
				xlab(,format(%3.2f) labsize(${labsize})) ylab(0(2)12, labsize(${labsize})) yscale(range(0 11)) title("${`cc'labp}", size(*1)) ///
				xline(`meanshare1', lcolor("$warm%60") lpattern(dash)) xline(`meanshare0', lcolor("$cold%60") lpattern(solid)) ///
				name(`cc'_f, replace) nodraw
				
	}			

	graph combine GHA_f MWI_f, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) 
	graph export "${figures}/Fig1.png", replace width(2000) as(png)
	graph export "${figures}/Fig1.eps", replace as(eps)
					
		

}


*----------------- Figure 2: Women's resource shares by age ----------------- *
if output ==4 {
	
	

	foreach cc in GHA MWI {
		use "${shares}/`cc'_model3.dta", clear
		replace avage_f=avage_f*10
		replace avage_f=70 if avage_f>70 & avage_f<.
		sum share_pf if kfm==1
		local sharefmean = r(mean)
		twoway  (lpolyci share_pf avage_f if patrilocal==1 & kfm==1 , lpattern(dash) lcolor("$warm") fcolor("${warm}%25") alcolor("${warm}%35") lwidth(*1.4) alwidth(none) bwidth(2.5) level(90)  ) ///
				(lpolyci share_pf avage_f if patrilocal==0 & kfm==1, lpattern(solid) lcolor("$cold") fcolor("${cold}%20") alcolor("${cold}%20") lwidth(*1.4) alwidth(none) bwidth(2.5) level(90) ) ///
				(scatteri `sharefmean' 17 (5) "Mean shares", msymbol(circle) mcolor(gs7) mlabcolor(gs8) msize(*.5) mlabsize(*.7) mlabgap(*.5)) ///			
				if avage_f>=18 & avage_f<=70 & kfm==1  , legend(order(2 "Patrilocal" 4 "Matrilocal") size(${labsize}) symxsize(*.7)) ytitle("Per woman resource shares", size(${labsize}) ) ///
				xtitle("Age of women", linegap(1.3) size(${labsize}) margin(t+2 b+1)) ///
				xlab(20(10)70, labsize(${labsize})) ylab(0.20(0.05)0.45,format(%3.2f) labsize(${labsize})) yscale(range(0.20 0.45)) xscale(range(17 70)) ///
				yline(`sharefmean', lcolor(gs9) lpattern(shortdash) lwidth(*1)) /// 
				title("${`cc'labp}", size(*1)) name(`cc'_fage, replace) nodraw
				
	}
	graph combine GHA_fage MWI_fage, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) 
	graph export "${figures}/Fig2.png", replace width(2000) as(png)
	graph export "${figures}/Fig2.eps", replace as(eps)	
						
}


*----------------- Figure 3: Heterogeneity of women's control ----------------- *
if output ==5 {
	
	* Ghana 
	use "${readydata}/GHA_women.dta", clear	
	gen age2=age^2
	gen patriXage=patrilocal*age
	reg decides age age2 patrilocal patriXage urban i.region 
	predict decides2, xb
	drop decides 
	rename decides2 decides 

	local ag="age"
	local fct="lpolyci"  

	twoway  (`fct' decides `ag' if patrilocal==1, lpattern(dash) lcolor("$warm") fcolor("${warm}%25") alcolor("${warm}%25") lwidth(*1.4) alwidth(none) bwidth(2.5) ) ///
			(`fct' decides `ag' if patrilocal==0, lpattern(solid) lcolor("$cold") fcolor("${cold}%20") alcolor("${cold}%20") lwidth(*1.4) alwidth(none) bwidth(2.5)  )	///
			, legend(order(2 "Patrilocal" 4 "Matrilocal") cols(2) size(*.8) symxsize(*.7)) ///
			xtitle("Average age of women", size(*.8) margin(t+2 b+1) linegap(1.3) ) ytitle("Control over own earnings", margin(r+1) size(*.8)) ///
			xlab(20(10)70, labsize(small)) ylab(0.80(0.05)1,format(%3.2f) labsize(small)) yscale(range(0.80 1)) xscale(range(17 70)) ///
			title("Ghana", size(*1)) name(GHA, replace) nodraw
			
	* Malawi
	use "${readydata}/MWI_women.dta", clear
	gen age2=age^2
	gen patriXage=patrilocal*age
	reg decides age age2 patrilocal patriXage  i.region  
	predict decides2, xb
	drop decides 
	rename decides2 decides 

	local ag="age" 
	local fct="lpolyci" // 

	twoway  (`fct' decides `ag' if patrilocal==1, lpattern(dash) lcolor("$warm") fcolor("${warm}%25") alcolor("${warm}%25") lwidth(*1.4) alwidth(none) bwidth(2.5) ) ///
			(`fct' decides `ag' if patrilocal==0, lpattern(solid) lcolor("$cold") fcolor("${cold}%20") alcolor("${cold}%20") lwidth(*1.4) alwidth(none) bwidth(2.5)  )	///
			, legend(order(2 "Patrilocal" 4 "Matrilocal") cols(2) size(${labsize}) symxsize(*.7)) ///
			xtitle("Average age of women", size(${labsize}) margin(t+2 b+1) linegap(1.3) ) ytitle("Control over own earnings", margin(r+1) size(${labsize})) ///
			xlab(20(10)70, labsize(${labsize})) ylab(0.80(0.05)1,format(%3.2f) labsize(${labsize})) yscale(range(0.80 1)) xscale(range(17 70)) ///
			title("Malawi", size(*1)) name(MWI, replace) nodraw
			
		
	graph combine GHA MWI, col(2) ycommon altshrink iscale(*1.35) xsize(2) ysize(1) 
	graph export "${figures}/Fig3.png", replace width(2000) as(png)
	graph export "${figures}/Fig3.eps", replace as(eps)			
		
}


*----------------- Poverty variables ----------------- *
if inlist(output,6,11)==1 {
	
	foreach cc in GHA MWI {
		
		use "${shares}/`cc'_model1.dta", clear
		
		if regexm("`cc'", "GHA")==1 { 
			gen ppp_cf = 1/0.778912  //for 2011, in survey 0.4626
			gen cpi_df = 1/1.64205  //1.64205 is from povcalnet, WDI CPI index: 108.728/241.062 if year==2017 // CPI_2011/CPI_year
		}
	
		if regexm("`cc'", "MWI")==1 { 
			gen ppp_cf = 1/78.703  // 2011 PPP exchange rate from WB: LCU per international dollars private consumption 
			gen cpi_df = 1/3.4569 // 3.4569 is from povcalnet, WDI CPI indices: 107.623/305.031 if year==2016 // CPI_2011/CPI_year
		}	
	
		* Total expenditure
		gen tot = rexp_wb*ppp_cf*cpi_df/365 //total expenditure used for WB poverty calculations
		gen logtot = (1+rexp_wb*ppp_cf*cpi_df) 
						
		* Adult equivalance scales for kids
		** per  capita
		gen nae1= nchild
		egen totae1 = rowtotal(nmen nwomen nae1)
		
		** nae => defined by age of child, FAO
		gen nae2= nae //nchild*0.6
		egen totae2 = rowtotal(nmen nwomen nae2)
		gen share_paek = share_k/nae2
		
		* Poverty line
		gen povline19 = 1.90 // PPP$ per day
		gen povline32 = 3.2 // PPP$ per day
		gen povline55 = 5.5 // PPP$ per day
		
		* Per capita/ adult equivalent HH expenditure
		gen tot_pc = tot/totae1
		gen tot_pae = tot/totae2
		gen tot_pk   = share_pk*tot
		gen tot_paek  = share_paek*tot
		gen tot_pf  = share_pf*tot
		gen tot_pm  = share_pm*tot
			
		gen logtot_pc = log(1+tot_pc)
		gen logtot_pae = log(1+tot_pae)
		
		* Poverty headcount
		foreach pov in 19 32 {
			foreach x in pc pae pk paek pf pm {
				gen poor`pov'_`x' = tot_`x'<povline`pov' if tot_`x'<.
			}
		}
		
		* Misclassification - Poor in non-poor households
		foreach pov in 19 32 {
			foreach x in pc pae {
				
				* Poor kids in non-poor HHs
				gen mispoor`pov'_pk_`x' = (poor`pov'_`x'==0) if poor`pov'_pk==1 
				
				* Poor adult eq. kids in non-poor HHs
				gen mispoor`pov'_paek_`x' = (poor`pov'_`x'==0) if poor`pov'_paek==1  
				
				* Poor women in non-poor HHs
				gen mispoor`pov'_pf_`x' = (poor`pov'_`x'==0) if poor`pov'_pf==1  
											
				* Poor men in non-poor HHs
				gen mispoor`pov'_pm_`x' = ( poor`pov'_`x'==0) if poor`pov'_pm==1 		
			
			}
		}	
		
		* Weighted poverty
		gen sweight_pc = sweight*totae1
		gen sweight_pae = sweight*totae2
		gen sweight_pk = sweight*nchild
		gen sweight_paek = sweight*nae
		gen sweight_pf = sweight*nwomen
		gen sweight_pm = sweight*nmen
		
		
		tempfile `cc'_pov
		save ``cc'_pov'
		
	}
	
							
}


*----------------- Figure 4 and A1: Poverty effects of patrilocality ----------------- *
if output==6 {

	global pov19=1.9
	global pov32=3.2

	foreach cc in GHA MWI {
		use ``cc'_pov', clear
		
		sum logtot_pae , d 									
		sum logtot_pae if kfm==1 , d  								
		sum logtot_pae if kfm==1 &  logtot_pae<3 , d 
		local medval = `r(p50)'

		foreach pov in 19 32 {
			foreach t in  f m aek {
				if ("`t'"=="k") local eta "Children"
				if ("`t'"=="aek") local eta "Children"
				if ("`t'"=="f") local eta "Women"
				if ("`t'"=="m") local eta "Men"
								
			twoway  (lpolyci poor`pov'_p`t' logtot_pae if patrilocal==1 & kfm==1   [aw=sweight_p`t'], lpattern(dash) lcolor("$warm") fcolor("${warm}%20") alcolor("${warm}%20") lwidth(*1.4) alwidth(none) bwidth(0.3)  ) ///
					(lpolyci poor`pov'_p`t' logtot_pae if patrilocal==0 & kfm==1  [aw=sweight_p`t'], lpattern(solid) lcolor("$cold") fcolor("${cold}%20") alcolor("${cold}%20") lwidth(*1.4) alwidth(none) bwidth(0.3) ) ///
					(scatteri 1 `medval' (3) "Median", msymbol(circle) mcolor(gs7) mlabcolor(gs6) msize(*.5) mlabsize(*.9) ) ///
					if logtot_pae>0 & logtot_pae<3 & kfm==1 , legend(order(2 "Patrilocal" 4 "Matrilocal") size(${labsize}) symxsize(*.7)) ytitle("`eta''s poverty rate" "{c $|}${pov`pov'}/day (2011 PPP)", linegap(1.3) size(${labsize}) ) ///
					xtitle("Log HH expenditure per adult eq./day""(2011 PPP dollars)", linegap(1.3) size(${labsize}) margin(t+2 b+1)) ///
					xlab(0(0.5)3,format(%2.1f) labsize(${labsize})) ylab(0(0.2)1,format(%2.1f) labsize(${labsize})) title("${`cc'lab}", size(*0.8)) ///
					xline(`medval', lcolor(gs5) lpattern(shortdash) lwidth(*.5)) /// 
					name(`cc'_pov`pov'_`t', replace) nodraw

			}					
		}
	}

	graph combine GHA_pov19_f  MWI_pov19_f, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) title("(A) Women", size(*1)) name(women, replace) nodraw
	graph combine GHA_pov19_m  MWI_pov19_m, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) title("(B) Men", size(*1)) name(men, replace) nodraw
	graph combine GHA_pov19_aek MWI_pov19_aek, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) title("(C) Children (adult eq.)", size(*1)) name(childrenaq, replace) nodraw
	
	graph combine women men childrenaq, xcommon altshrink cols(1) xsize(2) ysize(3) 
	graph export "${figures}/Fig4.png", replace width(2000) as(png)
	graph export "${figures}/Fig4.eps", replace as(eps)	
					
		
	graph combine GHA_pov32_f  MWI_pov32_f, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) title("(A) Women", size(*1)) name(women, replace) nodraw
	graph combine GHA_pov32_m  MWI_pov32_m, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) title("(B) Men", size(*1)) name(men, replace) nodraw
	graph combine GHA_pov32_aek MWI_pov32_aek, ycommon altshrink iscale(*1.35)  cols(2) xsize(2) ysize(1) title("(C) Children (adult eq.)", size(*1)) name(childrenaq, replace) nodraw
	
	graph combine women men childrenaq, xcommon altshrink cols(1) xsize(2) ysize(3) 
	graph export "${figures}/FigA1.png", replace width(2000) as(png)
	graph export "${figures}/FigA1.eps", replace as(eps)	
			

}


*----------------- Table A1: Summary statistics by HH type ----------------- *
if output==7 {
	
	putexcel set "${tables}/TabA1.xlsx", replace
	
	putexcel B1="Ghana" 
	putexcel B1:C1, merge hcenter
	putexcel D1="Malawi" 
	putexcel D1:E1, merge hcenter
	putexcel A2 = "Households with:" 
	putexcel B2 = "children, women and men"
	putexcel C2 = "women and men" 
	putexcel D2 = "children, women and men"
	putexcel E2 = "women and men" 
	
	
	local GHAcol_kfm = "B"
	local GHAcol_fm = "C"
	local MWIcol_kfm = "D"
	local MWIcol_fm = "E"

	foreach cc in GHA MWI {

		* Prepare data
		*********************
		use "${shares}/`cc'_model1.dta", clear

		
		if regexm("`cc'", "GHA")==1 { 
			gen ppp_cf = 1/0.778912  //for 2011, in survey 0.4626
			gen cpi_df = 1/1.64205  //1.64205 is from povcalnet, WDI CPI index: 108.728/241.062 if year==2017 // CPI_2011/CPI_year
		}

		if regexm("`cc'", "MWI")==1 { 
			gen ppp_cf = 1/78.703  // 2011 PPP exchange rate from WB: LCU per international dollars private consumption 
			gen cpi_df = 1/3.4569 // 3.4569 is from povcalnet, WDI CPI indices: 107.623/305.031 if year==2016 // CPI_2011/CPI_year
		}	
		
		* Zeros in clothing budget share
		gen zero_cloth_k = shexcl_k==0 if shexcl_k<. & nchild>0
		gen zero_cloth_f = shexcl_f==0 if shexcl_f<. & nwomen>0
		gen zero_cloth_m = shexcl_m==0 if shexcl_m<. & nmen>0

		* Correct private expenditure
		cap drop privexp_ppp
		gen privexp_ppp = privexp*ppp_cf*cpi_df //*cpi_df // 2011 PPP dollars
		cap lab var privexp_ppp "Annual HH expenditure (2011 PPP$)"
	
		* Correct back age variables
		foreach x in k f m {
			replace avage_`x'=avage_`x'*10
		}
			
			
		local hhchars "nchild nwomen nmen  avage_k avage_f avage_m pboy urban privexp_ppp"  //femaleratio pworking_f pemployee_f incomeratio ngirl nboy
		local bshares  " shexcl_k shexcl_f shexcl_m" 
		local zerocloth "zero_cloth_k zero_cloth_f zero_cloth_m" 
		
		
		* Summary statistics by HH type
		********************************
		** HH characteristics
		putexcel A3 = "Household characteristics" 
		local mrow = 4
		local sdrow = 5
		
		foreach var of varlist `hhchars'{ 
			* Label
			local varlab: var label `var'
			putexcel A`mrow' = "`varlab'"
			
			* Mean and SD for kfm
			sum `var' if kfm==1
			local mval: display %2.1f `r(mean)'
			local sdval: display %3.2f `r(sd)'
			if "`var'"=="privexp_ppp" {
				local mval: display %4.0f `r(mean)'
				local sdval: display %4.0f `r(sd)'
			}
			if inlist("`var'", "urban", "pboy")==1 {
				local mval: display %3.2f `r(mean)'
				local sdval: display %3.2f `r(sd)'
			
			}
			putexcel ``cc'col_kfm'`mrow' = `mval', hcenter
			putexcel ``cc'col_kfm'`sdrow' = "(`sdval')", hcenter
			
			* Mean and SD for fm	
			if inlist("`var'", "nchild", "avage_k", "pboy")!=1 {
				sum `var' if fm==1
				local mval: display %2.1f `r(mean)'
				local sdval: display %3.2f `r(sd)'
				if "`var'"=="privexp_ppp" {
					local mval: display %4.0f `r(mean)'
					local sdval: display %4.0f `r(sd)'
				}
				if inlist("`var'", "urban", "pboy")==1 {
					local mval: display %3.2f `r(mean)'
					local sdval: display %3.2f `r(sd)'
				}
				putexcel ``cc'col_fm'`mrow' = `mval', hcenter
				putexcel ``cc'col_fm'`sdrow' = "(`sdval')", hcenter
			}
			local mrow= `mrow'+2
			local sdrow= `sdrow'+2
		}
		
		** Budget shares of clothing
		putexcel A`mrow' = "Household budget share for clothing" 

		cap lab var shexcl_k "Children"
		cap lab var shexcl_f "Women"
		cap lab var shexcl_m "Men"
		
		local mrow = `mrow'+1
		local sdrow = `sdrow'+1
		foreach var of varlist `bshares'{ 
			* Label
			local varlab: var label `var'
			putexcel A`mrow' = "`varlab'"
			
			* Mean and SD for kfm
			sum `var' if kfm==1
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_kfm'`mrow' = `mval', hcenter
			putexcel ``cc'col_kfm'`sdrow' = "(`sdval')", hcenter
			
			* Mean and SD for fm	
			if inlist("`var'", "shexcl_k")!=1 {
				sum `var' if fm==1
				local mval: display %4.3f `r(mean)'
				local sdval: display %4.3f `r(sd)'
				putexcel ``cc'col_fm'`mrow' = `mval', hcenter
				putexcel ``cc'col_fm'`sdrow' = "(`sdval')", hcenter
			}
			local mrow= `mrow'+2
			local sdrow= `sdrow'+2
		}
		
		
		** Percentage of zeros in clothing budget share
		putexcel A`mrow' = "Percentage of zeros in clothing budget share" 
		
		cap lab var zero_cloth_k "Children"
		cap lab var zero_cloth_f "Women"
		cap lab var zero_cloth_m "Men"
		
		local mrow = `mrow'+1
		foreach var of varlist `zerocloth'{ 
			* Label
			local varlab: var label `var'
			putexcel A`mrow' = "`varlab'"
			
			* Mean and SD for kfm
			sum `var' if kfm==1
			local mval: display %4.3f `r(mean)'
			putexcel ``cc'col_kfm'`mrow' = `mval', hcenter
			
			* Mean and SD for fm	
			if inlist("`var'", "zero_cloth_k")!=1 {
				sum `var' if fm==1
				local mval: display %4.3f `r(mean)'
				putexcel ``cc'col_fm'`mrow' = `mval', hcenter
			}
			local mrow= `mrow'+1
		}
		
		** Sample size
		local mrow= `mrow'+1
		putexcel A`mrow' = "N" 
		count if kfm==1 
		putexcel ``cc'col_kfm'`mrow' = `r(N)', hcenter
		count if fm==1 
		putexcel ``cc'col_fm'`mrow' = `r(N)', hcenter
		
		
	}
		
	putexcel close	
}


*----------------- Table A2: Summary statistics by patrilocality----------------- *
if output==8 {
	
	
	putexcel set "${tables}/TabA2.xlsx", replace
	
	putexcel B1="Ghana" 
	putexcel B1:D1, merge hcenter
	putexcel E1="Malawi" 
	putexcel E1:G1, merge hcenter
	putexcel B2 = "Matrilocal"
	putexcel C2 = "Patrilocal" 
	putexcel D2 = "Diff."
	putexcel E2 = "Matrilocal"
	putexcel F2 = "Patrilocal" 
	putexcel G2 = "Diff."	
	
	local GHAcol_p0 = "B"
	local GHAcol_p1 = "C"
	local GHAcol_diff = "D"
	
	local MWIcol_p0 = "E"
	local MWIcol_p1 = "F"
	local MWIcol_diff = "G"
	
	foreach cc in GHA MWI {

		* Prepare data
		*********************
		use "${shares}/`cc'_model1.dta", clear

		
		if regexm("`cc'", "GHA")==1 { 
			gen ppp_cf = 1/0.778912  //for 2011, in survey 0.4626
			gen cpi_df = 1/1.64205  //1.64205 is from povcalnet, WDI CPI index: 108.728/241.062 if year==2017 // CPI_2011/CPI_year
		}

		if regexm("`cc'", "MWI")==1 { 
			gen ppp_cf = 1/78.703  // 2011 PPP exchange rate from WB: LCU per international dollars private consumption 
			gen cpi_df = 1/3.4569 // 3.4569 is from povcalnet, WDI CPI indices: 107.623/305.031 if year==2016 // CPI_2011/CPI_year
		}	
		
		* Zeros in clothing budget share
		gen zero_cloth_k = shexcl_k==0 if shexcl_k<. & nchild>0
		gen zero_cloth_f = shexcl_f==0 if shexcl_f<. & nwomen>0
		gen zero_cloth_m = shexcl_m==0 if shexcl_m<. & nmen>0

		* Correct private expenditure
		cap drop privexp_ppp
		gen privexp_ppp = privexp*ppp_cf*cpi_df //*cpi_df // 2011 PPP dollars
		cap lab var privexp_ppp "Annual HH expenditure (2011 PPP$)"
	
		* Correct back age variables
		foreach x in k f m {
			replace avage_`x'=avage_`x'*10
		}	
			
		local hhchars "nchild nwomen nmen  avage_k avage_f avage_m pboy urban privexp_ppp"  //femaleratio pworking_f pemployee_f incomeratio ngirl nboy
		local bshares  " shexcl_k shexcl_f shexcl_m" 
		local zerocloth "zero_cloth_k zero_cloth_f zero_cloth_m" 
		
		
		* Summary statistics by patrilocality
		********************************
		** HH characteristics
		putexcel A3 = "Household characteristics" 
		local mrow = 4
		local sdrow = 5
		
		foreach var of varlist `hhchars'{ 
			
			* Label
			local varlab: var label `var'
			putexcel A`mrow' = "`varlab'"
			
			reg `var' patrilocal if kfm==1, robust

			** Matrilocal
			local mval_p0: display %2.1f _b[_cons]
			local seval_p0: display %3.2f _se[_cons]
			if "`var'"=="privexp_ppp" {
				local mval_p0: display %4.0f _b[_cons]
				local seval_p0: display %2.0f _se[_cons]
			}
			if inlist("`var'", "urban", "pboy")==1 {
				local mval_p0: display %3.2f _b[_cons]
				local seval_p0: display %3.2f _se[_cons]			
			}
			putexcel ``cc'col_p0'`mrow' = "`mval_p0'", hcenter
			putexcel ``cc'col_p0'`sdrow' = "(`seval_p0')", hcenter
			
			** Difference
			local mval_diff: display %2.1f  _b[patrilocal]
			local seval_diff: display %3.2f  _se[patrilocal]
			if "`var'"=="privexp_ppp" {
				local mval_diff: display %4.0f _b[patrilocal]
				local seval_diff: display %2.0f _se[patrilocal]
			}
			if inlist("`var'", "urban", "pboy")==1 {
				local mval_diff: display %3.2f _b[patrilocal]
				local seval_diff: display %3.2f _se[patrilocal]			
			}
			putexcel ``cc'col_diff'`mrow' = "`mval_diff'", hcenter
			putexcel ``cc'col_diff'`sdrow' = "(`seval_diff')", hcenter
		
		
			** Patrilocal
			lincom patrilocal+_cons
			local mval_p1: display %2.1f `r(estimate)'
			local seval_p1: display %3.2f `r(se)'
			if "`var'"=="privexp_ppp" {
				local mval_p1: display %4.0f `r(estimate)'
				local seval_p1: display %2.0f `r(se)'
			}
			if inlist("`var'", "urban", "pboy")==1 {
				local mval_p1: display %3.2f `r(estimate)'
				local seval_p1: display %3.2f `r(se)'			
			}
			putexcel ``cc'col_p1'`mrow' = "`mval_p1'", hcenter
			putexcel ``cc'col_p1'`sdrow' = "(`seval_p1')", hcenter

			local mrow= `mrow'+2
			local sdrow= `sdrow'+2
		}
		
		** Budget shares of clothing
		putexcel A`mrow' = "Household budget share for clothing" 

		cap lab var shexcl_k "Children"
		cap lab var shexcl_f "Women"
		cap lab var shexcl_m "Men"
		
		local mrow = `mrow'+1
		local sdrow = `sdrow'+1
		foreach var of varlist `bshares'{ 
			* Label
			local varlab: var label `var'
			putexcel A`mrow' = "`varlab'"
			
			reg `var' patrilocal if kfm==1, robust

			** Matrilocal
			local mval_p0: display %4.3f _b[_cons]
			local seval_p0: display %4.3f _se[_cons]
			putexcel ``cc'col_p0'`mrow' = "`mval_p0'", hcenter
			putexcel ``cc'col_p0'`sdrow' = "(`seval_p0')", hcenter
			
			** Difference
			local mval_diff: display %4.3f  _b[patrilocal]
			local seval_diff: display %4.3f  _se[patrilocal]
			putexcel ``cc'col_diff'`mrow' = "`mval_diff'", hcenter
			putexcel ``cc'col_diff'`sdrow' = "(`seval_diff')", hcenter
		
			** Patrilocal
			lincom patrilocal+_cons
			local mval_p1: display %4.3f `r(estimate)'
			local seval_p1: display %4.3f `r(se)'
			putexcel ``cc'col_p1'`mrow' = "`mval_p1'", hcenter
			putexcel ``cc'col_p1'`sdrow' = "(`seval_p1')", hcenter

			local mrow= `mrow'+2
			local sdrow= `sdrow'+2
		}
		
		
		** Percentage of zeros in clothing budget share
		putexcel A`mrow' = "Percentage of zeros in clothing budget share" 
		
		cap lab var zero_cloth_k "Children"
		cap lab var zero_cloth_f "Women"
		cap lab var zero_cloth_m "Men"
		
		local mrow = `mrow'+1
		local sdrow= `sdrow'+1
		foreach var of varlist `zerocloth'{ 
			* Label
			local varlab: var label `var'
			putexcel A`mrow' = "`varlab'"
			
			reg `var' patrilocal if kfm==1, robust

			** Matrilocal
			local mval_p0: display %4.3f _b[_cons]
			local seval_p0: display %4.3f _se[_cons]
			putexcel ``cc'col_p0'`mrow' = "`mval_p0'", hcenter
			putexcel ``cc'col_p0'`sdrow' = "(`seval_p0')", hcenter
			
			** Difference
			local mval_diff: display %4.3f  _b[patrilocal]
			local seval_diff: display %4.3f  _se[patrilocal]
			putexcel ``cc'col_diff'`mrow' = "`mval_diff'", hcenter
			putexcel ``cc'col_diff'`sdrow' = "(`seval_diff')", hcenter
		
			** Patrilocal
			lincom patrilocal+_cons
			local mval_p1: display %4.3f `r(estimate)'
			local seval_p1: display %4.3f `r(se)'
			putexcel ``cc'col_p1'`mrow' = "`mval_p1'", hcenter
			putexcel ``cc'col_p1'`sdrow' = "(`seval_p1')", hcenter

			local mrow= `mrow'+2
			local sdrow= `sdrow'+2
		}
		
		** Sample size
		local mrow= `mrow'+1
		putexcel A`mrow' = "N" 
		count if kfm==1 & patrilocal==0
		putexcel ``cc'col_p0'`mrow' = `r(N)', hcenter
		count if kfm==1 & patrilocal==1
		putexcel ``cc'col_p1'`mrow' = `r(N)', hcenter
		
	}
	
	putexcel close
		
}


*----------------- Table A3: Robustness check ----------------- *
if output==9 {
		
	putexcel set "${tables}/TabA3.xlsx", replace
	
	putexcel B1="Ghana" 
	putexcel B1:D1, merge hcenter
	putexcel E1="Malawi" 
	putexcel E1:G1, merge hcenter
	putexcel B2 = "Baseline"
	putexcel C2 = "Geographic matching of ethnographic data" 
	putexcel D2 = "Alternative model specification"
	putexcel E2 = "Baseline"
	putexcel F2 = "Geographic matching of ethnographic data" 
	putexcel G2 = "Alternative model specification"
		
	putexcel A3 = "(a) Resource shares" 
	putexcel A4 = "Children"
	putexcel A6 = "Women"
	putexcel A8 = "Men" 
	putexcel A11 = "Per child"
	putexcel A13 = "Per woman"
	putexcel A15 = "Per man" 
	
	putexcel A17 = "(b) Marginal effect of patrilocality on per person resource shares"
	putexcel A18 = "Women's resource shares"
	putexcel A20 = "Children's resource shares"
	
	putexcel A23 = "% of HHs with non-flat Engel curve"
	putexcel A24 = "% of patrilocality"
	putexcel A25 = "N"
	

	* (a) Resource shares
	***************************

	local GHArob1 = "B"
	local GHArob2 = "C"
	local GHArob3 = "D"
	local MWIrob1 = "E"
	local MWIrob2 = "F"
	local MWIrob3 = "G"
		
	foreach cc in GHA MWI {
		forval r=1/3 {
			
			use "${shares}/`cc'_model`r'.dta", clear
			local mrow = 4
			local sdrow = 5
			local pmrow = 11
			local psdrow = 12
		
			foreach x in k f m {
				** per dem group
				sum share_`x' if kfm==1
				local share_`x'_kfm: disp %4.3f `r(mean)'
				local share_`x'_kfm_sd: disp %4.3f `r(sd)'	
				putexcel ``cc'rob`r''`mrow' = `share_`x'_kfm', hcenter
				putexcel ``cc'rob`r''`sdrow' = "(`share_`x'_kfm_sd')"	, hcenter
				local mrow=`mrow'+2
				local sdrow=`sdrow'+2
				
				** per person
				sum share_p`x' if kfm==1
				local share_p`x'_kfm: disp %4.3f `r(mean)'
				local share_p`x'_kfm_sd: disp %4.3f `r(sd)'
				putexcel ``cc'rob`r''`pmrow' = `share_p`x'_kfm', hcenter
				putexcel ``cc'rob`r''`psdrow' = "(`share_p`x'_kfm_sd')", hcenter	
				local pmrow=`pmrow'+2
				local psdrow=`psdrow'+2
			}
		}
	}
		
	* (b) Marginal effects
	***************************

	foreach cc in GHA MWI {
		forval r=1/3 {
		
			use "${shares}/`cc'_model`r'.dta", clear
			local b1row = 20
			local se1row = 21
			local b2row = 18
			local se2row = 19
			
			if (`r'==1) local var "patrilocal"
			if (`r'==2) local var "patrilocal_geo"
			if (`r'==3) local var "patrilocal"
			
			* Women's
			sum kfm_eta2_`var'_m
			local eta2_`var': disp %4.3f `r(mean)'
			sum kfm_eta2_`var'_mse
			local eta2_`var'_se: disp %4.3f `r(mean)'
			local eta2_`var'_st ""
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.64 & abs(`eta2_`var''/`eta2_`var'_se')<1.96) local eta2_`var'_st = "*" //10%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.96 & abs(`eta2_`var''/`eta2_`var'_se')<2.58) local eta2_`var'_st = "**" //5%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 2.58 ) local eta2_`var'_st = "***" //1%
			putexcel ``cc'rob`r''`b2row' = "`eta2_`var''`eta2_`var'_st'", hcenter
			putexcel ``cc'rob`r''`se2row' = "(`eta2_`var'_se')"	, hcenter
					
			* Children's 
			sum kfm_eta1_`var'_m
			local eta1_`var': disp %4.3f `r(mean)'
			sum kfm_eta1_`var'_mse
			local eta1_`var'_se: disp %4.3f `r(mean)'
			local eta1_`var'_st ""
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 1.64 & abs(`eta1_`var''/`eta1_`var'_se')<1.96) local eta1_`var'_st = "*" //10%
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 1.96 & abs(`eta1_`var''/`eta1_`var'_se')<2.58) local eta1_`var'_st = "**" //5%
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 2.58 ) local eta1_`var'_st = "***" //1%
			putexcel ``cc'rob`r''`b1row' = "`eta1_`var''`eta1_`var'_st'", hcenter
			putexcel ``cc'rob`r''`se1row' = "(`eta1_`var'_se')"	, hcenter
					
		}
	}
	
		
	* Other statistics
	***************************	
	foreach cc in GHA MWI {
		forval r=1/3 {
			
			use "${shares}/`cc'_model`r'.dta", clear
			local row=23
			foreach var in beta_pass patrilocal {
				sum `var' if kfm==1
				local `var'_mean: disp %4.3f `r(mean)'
				putexcel ``cc'rob`r''`row' = ``var'_mean', hcenter
				local row=`row'+1
			}
			
			count if kfm==1
			putexcel ``cc'rob`r''`row' = `r(N)', hcenter			
				
		}
	}

	putexcel close
	

}


*----------------- Table A4: Robustness check - patrilineality (only Ghana) ----------------- *
if output==10 {
		
	putexcel set "${tables}/TabA4.xlsx", replace
	
	putexcel B1="Ghana" 
	putexcel B1:C1, merge hcenter
	putexcel B2 = "children, women and men"
	putexcel C2 = "women and men" 
	
	putexcel A3 = "(a) Resource shares" 
	putexcel A4 = "Children"
	putexcel A6 = "Women"
	putexcel A8 = "Men" 
	putexcel A11 = "Per child"
	putexcel A13 = "Per woman"
	putexcel A15 = "Per man" 
	
	putexcel A18 = "(b) Marginal effects on per person resource shares"
	putexcel A19 = "Women's resource shares:"
	putexcel A20 = "Patrilineal (=1)"
	putexcel A22 = "Proportion of boys"
	putexcel A24 = "Urban (=1)" 
	putexcel A26 = "Children's resource shares:"
	putexcel A27 = "Patrilineal (=1)" 
	putexcel A29 = "Proportion of boys"
	putexcel A31 = "Urban (=1)" 
	
	putexcel A34 = "% of HHs with non-flat Engel curve"
	putexcel A35 = "% of patrilineality"
	putexcel A36 = "N"
	

	* (a) Resource shares
	***************************
	** kfm
	local GHAcol = "B"
	local MWIcol = "D"
	foreach cc in GHA {
		
		use "${shares}/`cc'_model4.dta", clear
		local mrow = 4
		local sdrow = 5
		local pmrow = 11
		local psdrow = 12
		
		foreach x in k f m {
			** per dem group
			sum share_`x' if kfm==1
			local share_`x'_kfm: disp %4.3f `r(mean)'
			local share_`x'_kfm_sd: disp %4.3f `r(sd)'	
			putexcel ``cc'col'`mrow' = `share_`x'_kfm', hcenter
			putexcel ``cc'col'`sdrow' = "(`share_`x'_kfm_sd')"	, hcenter
			local mrow=`mrow'+2
			local sdrow=`sdrow'+2
			
			** per person
			sum share_p`x' if kfm==1
			local share_p`x'_kfm: disp %4.3f `r(mean)'
			local share_p`x'_kfm_sd: disp %4.3f `r(sd)'
			putexcel ``cc'col'`pmrow' = `share_p`x'_kfm', hcenter
			putexcel ``cc'col'`psdrow' = "(`share_p`x'_kfm_sd')", hcenter	
			local pmrow=`pmrow'+2
			local psdrow=`psdrow'+2
		}
	}
	
	** fm 
	local GHAcol = "C"
	local MWIcol = "E"
	foreach cc in GHA  {
		
		use "${shares}/`cc'_model4.dta", clear
		local mrow = 6
		local sdrow = 7
		local pmrow = 13
		local psdrow = 14
		
		foreach x in f m {
			** per dem group
			sum share_`x' if fm==1
			local share_`x'_fm: disp %4.3f `r(mean)'
			local share_`x'_fm_sd: disp %4.3f `r(sd)'	
			putexcel ``cc'col'`mrow' = `share_`x'_fm', hcenter
			putexcel ``cc'col'`sdrow' = "(`share_`x'_fm_sd')"	, hcenter
			local mrow=`mrow'+2
			local sdrow=`sdrow'+2
			
			** per person
			sum share_p`x' if fm==1
			local share_p`x'_fm: disp %4.3f `r(mean)'
			local share_p`x'_fm_sd: disp %4.3f `r(sd)'
			putexcel ``cc'col'`pmrow' = `share_p`x'_fm', hcenter
			putexcel ``cc'col'`psdrow' = "(`share_p`x'_fm_sd')"	, hcenter
			local pmrow=`pmrow'+2
			local psdrow=`psdrow'+2
		}
	}
	
	* (b) Marginal effects
	***************************
	** kfm 
	local GHAcol = "B"
	local MWIcol = "D"
	
	foreach cc in GHA {
		
		use "${shares}/`cc'_model4.dta", clear
		local b1row = 27
		local se1row = 28
		local b2row = 20
		local se2row = 21
		
		foreach var in patrilineal pboy urban {
			* Women's
			sum kfm_eta2_`var'_m
			local eta2_`var': disp %4.3f `r(mean)'
			sum kfm_eta2_`var'_mse
			local eta2_`var'_se: disp %4.3f `r(mean)'
			local eta2_`var'_st ""
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.64 & abs(`eta2_`var''/`eta2_`var'_se')<1.96) local eta2_`var'_st = "*" //10%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.96 & abs(`eta2_`var''/`eta2_`var'_se')<2.58) local eta2_`var'_st = "**" //5%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 2.58 ) local eta2_`var'_st = "***" //1%
			putexcel ``cc'col'`b2row' = "`eta2_`var''`eta2_`var'_st'", hcenter
			putexcel ``cc'col'`se2row' = "(`eta2_`var'_se')"	, hcenter
			local b2row=`b2row'+2
			local se2row=`se2row'+2	
			
			
			* Children's 
			sum kfm_eta1_`var'_m
			local eta1_`var': disp %4.3f `r(mean)'
			sum kfm_eta1_`var'_mse
			local eta1_`var'_se: disp %4.3f `r(mean)'
			local eta1_`var'_st ""
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 1.64 & abs(`eta1_`var''/`eta1_`var'_se')<1.96) local eta1_`var'_st = "*" //10%
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 1.96 & abs(`eta1_`var''/`eta1_`var'_se')<2.58) local eta1_`var'_st = "**" //5%
			if (abs(`eta1_`var''/`eta1_`var'_se')>= 2.58 ) local eta1_`var'_st = "***" //1%
			putexcel ``cc'col'`b1row' = "`eta1_`var''`eta1_`var'_st'", hcenter
			putexcel ``cc'col'`se1row' = "(`eta1_`var'_se')"	, hcenter
			local b1row=`b1row'+2
			local se1row=`se1row'+2	
			
		}
		
	}
	
	
	** fm 
	local GHAcol = "C"
	local MWIcol = "E"
	
	foreach cc in GHA  {
		
		use "${shares}/`cc'_model4.dta", clear
		local b1row = 27
		local se1row = 28
		local b2row = 20
		local se2row = 21
		
		foreach var in patrilineal urban {
			* Women's
			sum fm_eta2_`var'_m
			local eta2_`var': disp %4.3f `r(mean)'
			sum fm_eta2_`var'_mse
			local eta2_`var'_se: disp %4.3f `r(mean)'
			local eta2_`var'_st ""
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.64 & abs(`eta2_`var''/`eta2_`var'_se')<1.96) local eta2_`var'_st = "*" //10%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 1.96 & abs(`eta2_`var''/`eta2_`var'_se')<2.58) local eta2_`var'_st = "**" //5%
			if (abs(`eta2_`var''/`eta2_`var'_se')>= 2.58 ) local eta2_`var'_st = "***" //1%
			putexcel ``cc'col'`b2row' = "`eta2_`var''`eta2_`var'_st'", hcenter
			putexcel ``cc'col'`se2row' = "(`eta2_`var'_se')"	, hcenter
			local b2row=`b2row'+4
			local se2row=`se2row'+4	
			
		}
		
	}
	
	
		
	* Other statistics
	***************************
	local GHAcol_kfm = "B"
	local MWIcol_kfm = "D"
	local GHAcol_fm = "C"
	local MWIcol_fm = "E"	
	foreach cc in GHA {
		use "${shares}/`cc'_model4.dta", clear
		foreach t in kfm fm {
			local row=34
			foreach var in beta_pass patrilineal {
				sum `var' if `t'==1
				local `var'_mean: disp %4.3f `r(mean)'
				putexcel ``cc'col_`t''`row' = ``var'_mean', hcenter
				local row=`row'+1
			}
			
			count if `t'==1
			putexcel ``cc'col_`t''`row' = `r(N)', hcenter			
			
		}
	}
	
	putexcel close
	

}


*----------------- Table A5: Poverty estimates ----------------- *
if output==11 {
	
	local pk_lab "Children (per child)"
	local paek_lab "Children (per adult eq.)"
	local pf_lab "Women"
	local pm_lab "Men"
	
	putexcel set "${tables}/TabA5.xlsx", replace
	
	putexcel B1="Ghana" 
	putexcel B1:D1, merge hcenter
	putexcel E1="Malawi" 
	putexcel E1:G1, merge hcenter
	putexcel B2 = "All"
	putexcel C2 = "Matrilocal" 
	putexcel D2 = "Patrilocal"
	putexcel E2 = "All"
	putexcel F2 = "Matrilocal" 
	putexcel G2 = "Patrilocal"	

	
	local GHAcol_a = "B"
	local GHAcol_m = "C"
	local GHAcol_p = "D"
	
	local MWIcol_a = "E"
	local MWIcol_m = "F"
	local MWIcol_p = "G"
	
	foreach cc in GHA MWI { //
	
		use ``cc'_pov', clear
		
		* (a) Household poverty rates
		********************************
		putexcel A3 = "(a) Household poverty rates (%)" 
		putexcel A4 = "Per capita"
		putexcel A6 = "Per adult eq."
		local mrow = 4
		local sdrow = 5
		
		foreach x in pc pae {
			sum poor19_`x' if all==1 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_a'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_a'`sdrow' = "(`sdval')", hcenter
			
			sum poor19_`x' if all==1 & patrilocal==0 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_m'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_m'`sdrow' = "(`sdval')", hcenter
			
			sum poor19_`x' if all==1 & patrilocal==1 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_p'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_p'`sdrow' = "(`sdval')", hcenter
			
			local mrow = `mrow'+2
			local sdrow = `sdrow'+2
				
		}
		
		* (b) World Bank poverty rate
		********************************
		local mrow = `mrow'+1
		local sdrow = `mrow'+1		
		putexcel A`mrow' = "(b) World Bank poverty rate (%)"
		putexcel B`mrow' = 0.127, hcenter //WB poverty rate for Ghana at 1.9
		putexcel E`mrow' = 0.692, hcenter //WB poverty rate for Malawi at 1.9 	
		local mrow = `mrow'+2
		local sdrow = `mrow'+2
		
		* (c) Individual poverty rates
		********************************
		putexcel A`mrow' = "(c) Individual poverty rates (%)"
		local mrow = `mrow'+1
		local sdrow = `mrow'+1
		
		foreach x in pk paek pf pm {
			putexcel A`mrow' = "``x'_lab'"
			
			sum poor19_`x' if all==1 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_a'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_a'`sdrow' = "(`sdval')", hcenter
			
			sum poor19_`x' if all==1 & patrilocal==0 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_m'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_m'`sdrow' = "(`sdval')", hcenter
			
			sum poor19_`x' if all==1 & patrilocal==1 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_p'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_p'`sdrow' = "(`sdval')", hcenter
			
			local mrow = `mrow'+2
			local sdrow = `sdrow'+2
				
		}
		
		
		* (d) Misclassification
		********************************
		local mrow = `mrow'+1
		local sdrow = `mrow'+1	
		putexcel A`mrow' = "(d) Misclassification (fraction of poor individuals in non-poor households)"
		local mrow = `mrow'+1
		local sdrow = `mrow'+1
		
		foreach x in pk paek pf pm {
			putexcel A`mrow' = "``x'_lab'"

			sum mispoor19_`x'_pae if all==1 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_a'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_a'`sdrow' = "(`sdval')", hcenter
			
			sum mispoor19_`x'_pae if all==1 & patrilocal==0 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_m'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_m'`sdrow' = "(`sdval')", hcenter
			
			sum mispoor19_`x'_pae if all==1 & patrilocal==1 [aw=sweight_`x']
			local mval: display %4.3f `r(mean)'
			local sdval: display %4.3f `r(sd)'
			putexcel ``cc'col_p'`mrow' = "`mval'", hcenter
			putexcel ``cc'col_p'`sdrow' = "(`sdval')", hcenter
			
			local mrow = `mrow'+2
			local sdrow = `sdrow'+2
				
		}
		
		* Other statistics
		********************************
		local mrow = `mrow'+1
		putexcel A`mrow' = "N"
		count if all==1
		putexcel ``cc'col_a'`mrow' = "`r(N)'", hcenter
		count if patrilocal==0
		putexcel ``cc'col_m'`mrow' = "`r(N)'", hcenter
		count if patrilocal==1
		putexcel ``cc'col_p'`mrow' = "`r(N)'", hcenter

	}
	
	putexcel close 
}










